package com.web.oa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.oa.mapper.SysPermissionMapper;
import com.web.oa.mapper.SysPermissionMapper2;
import com.web.oa.mapper.SysRoleMapper;
import com.web.oa.mapper.SysRolePermissionMapper;
import com.web.oa.pojo.*;
import com.web.oa.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class SysServiceImpl implements SysService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysPermissionMapper2 sysPermissionMapper2;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public List<MenuTree> findMenuTree() {
        return sysPermissionMapper2.selectMenuTrees();
    }

    @Override
    public List<SysPermission> findPermissionListByEmployeeId(String name) throws Exception {
        return sysPermissionMapper2.selectPermissionListByEmployeeId(name);
    }

    @Override
    public PageInfo<SysRole> findAllRoles(int pageNum) {
        PageHelper.startPage(pageNum,5);
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(null);
        return new PageInfo<>(sysRoles);
    }

    @Override
    public List<SysRole> findAllRoles() {
        return sysRoleMapper.selectByExample(null);
    }

    @Override
    public List<MenuTree> findAllMenuTrees() {
        return sysPermissionMapper2.selectAllMenuTrees();
    }

    @Override
    public List<SysPermission> findPermissionListByRoleId(String roleId) {
        return sysPermissionMapper2.selectPermissionListByRoleId(roleId);
    }

    //因为是多条记录，所以先删除，后添加
    @Override
    public void updateRoleAndPermissionByRoleId(String roleId, int[] permissionIds) {
        SysRolePermissionExample example = new SysRolePermissionExample();
        SysRolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andSysRoleIdEqualTo(roleId);
        sysRolePermissionMapper.deleteByExample(example);

        for (int permissionId : permissionIds) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(UUID.randomUUID().toString());
            sysRolePermission.setSysRoleId(roleId);
            sysRolePermission.setSysPermissionId(String.valueOf(permissionId));
            sysRolePermissionMapper.insert(sysRolePermission);
        }
    }

    @Override
    public SysRole findRoleAndPermissionListByUserName(String userName) {
        return sysPermissionMapper2.selectRoleAndPermissionListByUserName(userName);
    }

    @Override
    public List<SysPermission> findAllMenu() {
        SysPermissionExample example = new SysPermissionExample();
        SysPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo("menu");
        return sysPermissionMapper.selectByExample(example);
    }

    @Override
    public List<SysRole> findRoleAndPermissionList() {
        return sysPermissionMapper2.selectRoleAndPermissionList();
    }

    @Override
    public void addRoleAndPermissions(SysRole role, int[] permissionIds) {
        sysRoleMapper.insert(role);
        if (permissionIds == null){
            return;
        }
        for (int permissionId : permissionIds) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(UUID.randomUUID().toString());
            sysRolePermission.setSysPermissionId(String.valueOf(permissionId));
            sysRolePermission.setSysRoleId(role.getId()+"");
            sysRolePermissionMapper.insert(sysRolePermission);
        }

    }

    @Override
    public void addSysPermission(SysPermission permission) {
        sysPermissionMapper.insert(permission);
    }
}
